package com.wh.wisdomsite.xbox.server.task.process;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.server.util.DateUtils;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.wh.wisdomsite.xbox.server.task.base.BaseTask;




public class TaskProcessor {


	private static final Log logger = LogFactory.getLog(TaskProcessor.class);
	
	@Autowired
	private HashMap<String, BaseTask> taskMap;
	
	private static int threadPoolNum = 20;

	private final ScheduledExecutorService scheduExec = Executors.newScheduledThreadPool(threadPoolNum);

	private TaskProcessor() {

	}

	private static class TaskProcessorHolder {
		private static TaskProcessor instance = new TaskProcessor();
	}

	private static TaskProcessor getInstance() {
		return TaskProcessorHolder.instance;
	}

	public synchronized void init() {
		TaskDate.setScheduledFutureMap(new HashMap<String, Object>());
		if (!TaskDate.isInitFlag()) {
			logger.info("TaskProcessor->>初始化init->>开始");
			long initDelay = 0;
			for (Entry<String, BaseTask> entry : taskMap.entrySet()) {
				ScheduledFuture<?> sche = null;
				int type = -1;
				String strFixedTime = "";
				Map <String,Object> taskValueMap = new HashMap<String, Object>(); 
				String description = entry.getValue().getDescription();
				long Period = entry.getValue().getPeriod();
				// 间隔周期执行
				if (StringUtils.isEmpty(entry.getValue().getFixedTime())) {
					logger.info("TaskProcessor->>按间隔周期执行");
					strFixedTime = "--:--:--";
					initDelay = entry.getValue().getInitialDelay();
					//从现在开始 initDelay 秒钟之后，每隔 Period 秒钟执行一次
					sche = getInstance().scheduExec.scheduleAtFixedRate(entry.getValue(), initDelay, Period, TimeUnit.SECONDS);
					type = Constants.TASK_STATUS_0;
				}
				// 固定时间点执行
				else {
					logger.info("TaskProcessor->>固定时间点执行");
					strFixedTime = entry.getValue().getFixedTime();
					long currentTime = System.currentTimeMillis();
					long fixedTime = DateUtils.getTimeMillis(strFixedTime);
					initDelay = fixedTime - currentTime;
					initDelay = initDelay > 0 ? initDelay : Period + initDelay;
					//从现在开始 initDelay 毫秒之后，每隔 Period 毫秒执行一次
					sche = getInstance().scheduExec.scheduleAtFixedRate(entry.getValue(), initDelay, Period, TimeUnit.MILLISECONDS);//MILLISECONDS
					type = Constants.TASK_STATUS_1;
				}
				taskValueMap.put("description", description);
				taskValueMap.put("period", Period);
				taskValueMap.put("type", type);
				taskValueMap.put("initDelay", initDelay);
				taskValueMap.put("sche", sche);
				taskValueMap.put("strFixedTime", strFixedTime);
				//存储task任务
				TaskDate.getScheduledFutureMap().put(entry.getKey(), taskValueMap);
			}
			TaskDate.setInitFlag(true);
			logger.info("TaskProcessor->>初始化init->>结束");
		}
	}
}
